// pages/gonglue/gonglue.js
//获取应用实例
var app = getApp()
var that;
var myDate = new Date();
//格式化日期
function formate_data(myDate) {
  let month_add = myDate.getMonth() + 1;
  var formate_result = myDate.getFullYear() + '-' +
    month_add + '-' +
    myDate.getDate()
  return formate_result;
}
Page({
  /**
   * 页面的初始数据
   */
  data: {
    date: formate_data(myDate),
    address: '点击选择位置',
    longitude: 0, //经度
    latitude: 0, //纬度
    noteMaxLen: 200, //备注最多字数
    content: "",
    noteNowLen: 0, //备注当前字数
    imgUrls: [
      'https://7465-test-4e6095-1258086226.tcb.qcloud.la/庐山/云海.jpg?',
      'https://7465-test-4e6095-1258086226.tcb.qcloud.la/庐山/三叠泉.jpg?',
      'https://7465-test-4e6095-1258086226.tcb.qcloud.la/庐山/庐山.jpg?',
      'https://7465-test-4e6095-1258086226.tcb.qcloud.la/庐山/含鄱口.jpg?',
      'https://7465-test-4e6095-1258086226.tcb.qcloud.la/庐山/植物园.jpg?',
      'https://7465-test-4e6095-1258086226.tcb.qcloud.la/庐山/锦绣谷.jpg?',
      'https://7465-test-4e6095-1258086226.tcb.qcloud.la/庐山/卢林湖.jpg?',
    ],
    indicatorDots: true,
    vertical: false,
    autoplay: true,
    circular: false,
    interval: 2000,
    duration: 500,
    previousMargin: 0,
    nextMargin: 0
  },



  //字数改变触发事件
  bindTextAreaChange: function(e) {
    var that = this
    var value = e.detail.value,
      len = parseInt(value.length);
    if (len > that.data.noteMaxLen)
      return;
    that.setData({
      content: value,
      noteNowLen: len
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    that = this;
    that.setData({ //初始化数据
      src: "",
      isSrc: false,
      ishide: "0",
      autoFocus: true,
      isLoading: false,
      loading: true,
      isdisabled: false
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    wx.hideToast()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var myInterval = setInterval(getReturn, 500); ////半秒定时查询
    function getReturn() {
      wx.getStorage({
        key: 'user_openid',
        success: function(ress) {
          if (ress.data) {
            clearInterval(myInterval)
            that.setData({
              loading: true
            })
          }
        }
      })
    }
  },



  //改变时间
  bindDateChange: function(e) {
    this.setData({
      date: e.detail.value
    })
  },
 
  //选择地点
  addressChange: function (e) {
    this.addressChoose(e);
  },
  addressChoose: function (e) {
    var that = this;
    wx.chooseLocation({
      success: function (res) {
        that.setData({
          address: res.name,
          longitude: res.longitude, //经度
          latitude: res.latitude,//纬度
        })
        if (e.detail && e.detail.value) {
          this.data.address = e.detail.value;
        }
      },
      fail: function (e) {
      },
      complete: function (e) {
      }
    })
  },



  //表单验证
  showTopTips: function() {
    var that = this;
    this.setData({
      showTopTips: true
    });
    setTimeout(function() {
      that.setData({
        showTopTips: false
      });
    }, 3000);
  },

  /**
   * 新增记录
   */
  adduser: function(e) {
    wx.showModal({
      title: '提示',
      content: '确定信息无误，提交信息吗',
      success: function(res) {
        if (res.confirm) {
          const db = wx.cloud.database()
          db.collection('goliv').add({
            data: e.detail.value,
            success: res => {
              // 在返回结果中会包含新创建的记录的 _id
              this.setData({
                counterId: res._id,
                count: 1
              })
              console.log('[数据库] [新增记录] 成功，记录 _id: ', res._id)
            },
            fail: err => {
              wx.showToast({
                icon: 'none',
                title: '新增记录失败'
              })
              console.error('[数据库] [新增记录] 失败：', err)
            }
          })
          wx.showToast({
            title: '获取攻略成功',
          })
          //重置表单
          that.setData({
            address: '点击选择位置',
            longitude: 0, //经度
            latitude: 0, //纬度
            data: formate_data(myDate),
            isHide: true,
            peoplenum: 0,
            peopleHide: false,
            title: 0,
            peopleHide: false,
            noteMaxLen: 200, //备注最多字数
            content: "",
            noteNowLen: 0, //备注当前字数
            imgUrls: [
              'https://7465-test-4e6095-1258086226.tcb.qcloud.la/庐山/云海.jpg?',
              'https://7465-test-4e6095-1258086226.tcb.qcloud.la/庐山/三叠泉.jpg?',
              'https://7465-test-4e6095-1258086226.tcb.qcloud.la/庐山/庐山.jpg?',
              'https://7465-test-4e6095-1258086226.tcb.qcloud.la/庐山/含鄱口.jpg?',
              'https://7465-test-4e6095-1258086226.tcb.qcloud.la/庐山/植物园.jpg?',
              'https://7465-test-4e6095-1258086226.tcb.qcloud.la/庐山/锦绣谷.jpg?',
              'https://7465-test-4e6095-1258086226.tcb.qcloud.la/庐山/卢林湖.jpg?',
            ],
            indicatorDots: true,
            vertical: false,
            autoplay: true,
            circular: false,
            interval: 2000,
            duration: 500,
            previousMargin: 0,
            nextMargin: 0,

          })
          console.log('[数据库] [新增记录] 成功，记录 _id: ', res._id)
        }
      }
    })
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})